<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>登录页</h1>
    <label for="username">用户名</label>
    <input id="username" type="text"/>

    <label for="password">密码</label>
    <input id="password" type="password"/>

<button id="login">登录</button>
</body>
<script type="text/javascript">
    //获取当前网址，如： http://localhost:8080/ems/Pages/Basic/Person.jsp
    var url = window.document.location.href;
    //获取主机地址之后的目录，如： /ems/Pages/Basic/Person.jsp
    var pathName = window.document.location.pathname;
    var pos = url.indexOf(pathName);
    //获取主机地址，如： http://localhost:8080
    var localhostPath = url.substring(0, pos);
    //获取带"/"的项目名，如：/ems
    var projectName = pathName.substring(0, pathName.indexOf('/', 1) + 1);
    var basePath = localhostPath + projectName;

    $("#login").click(function () {
        var userMO = {
            userName: $("#username").val(),
            userPassword: $("#password").val()
        };
        $.ajax({
            url: '/page/loginInfo',
            data: JSON.stringify(userMO),
            method: 'POST',
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            success: function (e) {
                if (e.code == "0") {
                    window.location.href = localhostPath + "/page/index";
                } else {
                    window.location.href = localhostPath + "/page/login";
                }
            },
            error: function (e) {
                window.location.href = localhostPath + "/page/error";
            }
        })
    })
</script>
</html>